/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package serverClasses;

import commonClasses.Messages.AckMsg;
import commonClasses.Messages.NakMsg;
import commonClasses.Messages.NewBidMsg;
import commonClasses.commonInterfaces.HostModelIntf;
import commonClasses.commonInterfaces.ItemIntf;
import commonClasses.commonInterfaces.ProtocolIntf;
/**
 *
 * @author MGL
 */
public class NewBidMsgService {

    private ProtocolIntf _protocol;
    private HostModelIntf _model;
    private String _userHash;

    public NewBidMsgService(ProtocolIntf _protocol, String userHash) {
        this._protocol = _protocol;
        this._model = _protocol.getHostModel();
    }

    public void execute() throws Exception {
        NewBidMsg newBidMsg = new NewBidMsg(_protocol);
        try {
            newBidMsg.receive();
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        ItemIntf item = newBidMsg.getItem();
        boolean result;
        if (_userHash != null) {
            result = _model.processNewBid(_userHash, item.getId(), item.getPrice());
        } else {
            result = false;
        
        }
        if (result) {
            new AckMsg(_protocol).send();
        } else {
            new NakMsg(_protocol, "niestety nie udalo sie").send();
        }
    }
}
